package dormouse.vfs.ui.renderers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.table.AbstractTableModel;

/**
 * This class creates a table model used to represent a series of property key/value pairs.
 * 
 * @author Mark
 *
 */
public class MetadataTableModel extends AbstractTableModel {

    /** Serial UID. */
	private static final long serialVersionUID = 2620499001825632698L;
	
	List<List<String>> dataList = new ArrayList<List<String>>();

    /**
     * Constructor
     */
    public MetadataTableModel() {
        super();
    }

    /**
     * Constructor
     * @param properties a map of metadata key-value pairs.
     */
    public MetadataTableModel(Map<String, String> properties) {
        Set<String> keys = properties.keySet();
        Iterator<String> it = keys.iterator();
        String currKey = null;
        List<String> currRow = null;
        while (it.hasNext()) {
            currKey = (String) it.next();
            currRow = new ArrayList<String>();
            currRow.add(currKey);
            currRow.add(properties.get(currKey));
            this.dataList.add(currRow);
        }
    }

    @Override
    public Object getValueAt(int row, int column) {
        List<List> rowData = (List) this.dataList.get(row);
        return rowData.get(column);
    }

    @Override
    public void setValueAt(Object value, int row, int column) {
        List rowData = (List) this.dataList.get(row);
        rowData.set(column, value);
    }

    @Override
    public Class getColumnClass(int col) {
        return String.class;
    }

    @Override
    public int getColumnCount() {
        return colnames.length;
    }

    @Override
    public int getRowCount() {
        return this.dataList.size();
    }

    @Override
    public String getColumnName(int col) {
        return this.colnames[col];
    }
    String[] colnames = new String[]{"Property", "Value"};
}
